<?php

namespace Biz\Middleware;

use Closure;
use Biz\Constants\ApplicationConstants;

/**
 * Localization.
 *
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class Localization
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure                 $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Check header request and determine localizaton
        $locale = ($request->hasHeader(ApplicationConstants::HEADER_X_APP_LOCALIZATION)) ? $request->header(ApplicationConstants::HEADER_X_APP_LOCALIZATION) : 'zh-CN';
        // Set lumen localization
        $request->setLocale($locale);
        app('config')->set('app.locale', $locale);
        app('config')->set('translatable.locale', $locale);
        app('translator')->setLocale($locale);
        app('events')->fire('locale.changed', [$locale]);

        // Continue request
        return $next($request);
    }
}
